extends Node2D

@onready var player = $PlayerInBattle
@onready var card_table = $CardTable
@onready var prop_bar = $UICanvasLayer/PropBar
@onready var monster_handler = $MonsterHandler
@onready var statue = $Statue
@onready var battle_state_machine = $BattleStateMachine as BattleStateMachine

var current_monster_group_id


func _ready():
	Globals.player_in_battle = $PlayerInBattle
	Globals.monster_handler = monster_handler
	TestGlobals.monster_handler = monster_handler
	
	battle_state_machine.init()
	monster_handler.child_order_changed.connect(_on_monster_child_order_changed)
	player.position = $PlayerPlaceholder.position
	player.placeholder = $PlayerPlaceholder
	var id_array = MonsterManager.MONSTER_GROUP_DICT[current_monster_group_id]
	for i in range(0, id_array.size()):
		var monster: Monster = MonsterManager.generate_monster(id_array[i])
		if i == 0:
			monster.position = $MonsterPlaceholder0.position
			monster.placeholder = $MonsterPlaceholder0
		if i == 1:
			monster.position = $MonsterPlaceholder1.position
			monster.placeholder = $MonsterPlaceholder1
		if i == 2:
			monster.position = $MonsterPlaceholder2.position
			monster.placeholder = $MonsterPlaceholder2
		if i == 3:
			monster.position = $MonsterPlaceholder3.position
			monster.placeholder = $MonsterPlaceholder3
		if i == 4:
			monster.position = $MonsterPlaceholder4.position
			monster.placeholder = $MonsterPlaceholder4
		monster.z_index = 0
		monster_handler.add_child(monster)



func _on_monster_child_order_changed():
	if monster_handler.get_child_count() == 0:
		var tween = create_tween()
		if tween != null:
			tween.tween_callback(card_table.on_battle_win)
			tween.tween_interval(0.4)
			tween.finished.connect(func(): Events.battle_win.emit())
